import request from '@/config/axios'
import { Personal, Share } from '@/utils/config'
import CryptoJS from 'crypto-js'

/**
 * 素材库类型，personal：个人素材，share：共享素材
 */
type MaterialType = Personal | Share

// 查询个人素材图片列表
export const getPersonalImagePage = async (params: PageParam) => {
  return await request.get({ url: '/resource/1/personal/list', params })
}

// 查询所有文件附件
export const queryFilePage = async (params: PageParam) => {
  return await request.get({ url: '/resource/0/page', params })
}

// 查询所有文件附件
export const download = async (resourceId, waterId) => {
  return await request.download({ url: '/resource/download/' + resourceId + '/' + waterId })
}

/**
 * 素材库图片上传，默认personal
 * @param data
 * @param materialType
 */
export const uploadImage = (data, materialType: MaterialType = Personal) => {
  return request.upload({ url: '/resource/1/' + materialType + '/upload', data: data })
}

export const uploadFile = (data, materialType: MaterialType = Personal) => {
  return request.upload({ url: '/resource/0/' + materialType + '/upload', data: data })
}

export const selectList = async (ids: string) => {
  return await request.get({ url: '/resource/selectList?ids=' + ids })
}

export const saveBookHouseFile = async (type: number, dataId: number, data) => {
  return await request.post({
    url: '/resource/saveBookHouseFile/' + dataId + '/' + type,
    data: data
  })
}

/**
 * 图片保存
 * @param data
 */
export const saveImageList = async (data) => {
  return await request.post({ url: '/resource/save', data: data })
}

export const selectByDataFieldId = async (id) => {
  return await request.get({ url: '/resource/select/' + id })
}

export const getFileMd5 = (file: File) => {
  const reader = new FileReader()
  return new Promise((resolve) => {
    const fileReader = new FileReader()
    fileReader.onloadend = (ev) => {
      resolve(CryptoJS.MD5(CryptoJS.enc.Latin1.parse(ev.target.result)).toString(CryptoJS.enc.Hex))
    }
    fileReader.readAsBinaryString(file)
  })
}

//导入进度条接口
export const getupload = async (file: File) => {
  return await request.post({ url: `/uploadOld?file=${file}` })
}
